package com.xiaomei.manager.quartz;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;


import com.xiaomei.manager.dao.WeixinBillMapper;
import com.xiaomei.manager.model.WeixinBill;
import com.xiaomei.manager.weixin.entity.DownloadBillDto;
import com.xiaomei.manager.weixin.util.ConstantUtil;
import com.xiaomei.manager.weixin.util.HttpXmlUtil;
import com.xiaomei.manager.weixin.util.WXSignUtils;

/**
 * 每天5点执行，将对账单数据下载到数据库的t_weixin_bill表中
 * @author xiaozsheng
 *
 */

public class QuartzJobDownBill {
	@Autowired
	private WeixinBillMapper wxBillMapper; 
	
	private Logger logger = Logger.getLogger(QuartzJobDownBill.class);
	private static int counter = 0;
	protected void execute(){
		
		List<WeixinBill> list = new ArrayList<WeixinBill>();	
		String requestUrl = "https://api.mch.weixin.qq.com/pay/downloadbill";
		String requestMethod = "POST";
		String billType = "ALL";
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		String billDate = "20160908";
		
		SortedMap<Object, Object> parameters  = new TreeMap<Object, Object>();
		String appid = ConstantUtil.APP_ID;
		String mch_id = ConstantUtil.PARTNER;
		String noncestr = WXSignUtils.getNonceStr();
		parameters.put("appid", appid);
		parameters.put("mch_id", mch_id);
		parameters.put("nonce_str", noncestr);
		parameters.put("bill_date", billDate);
		parameters.put("bill_type", billType);
		String sign = WXSignUtils.createSign("utf-8", parameters);
		
		DownloadBillDto dto = new DownloadBillDto();
		dto.setAppid(appid);
		dto.setBill_date(billDate);
		dto.setBill_type(billType);
		dto.setMch_id(mch_id);
		dto.setNonce_str(noncestr);
		dto.setSign(sign);
		
		String xml = HttpXmlUtil.xmlInfo(dto);
		System.out.println("请求xml内容："+ xml);
		String result = HttpXmlUtil.httpsRequest(requestUrl, requestMethod, xml);
		System.out.println("result："+ result);
		if(result.startsWith("<xml>")){			
			System.out.println("无对账单信息");
		}else{
			String[] resultContent = result.split("\\r?\\n");
			for(int i=1; i<resultContent.length - 1; i++){
				if(i >= resultContent.length - 3){  
                    break;  
                }
				if(!resultContent[i].equals("")){
					String[] values = resultContent[i].split(",");
					WeixinBill wb = new WeixinBill();
					wb.setTradetime(values[0]);
					wb.setGhid(values[1]);
					wb.setMchid(values[2]);
					wb.setSubmch(values[3]);
					wb.setDeviceid(values[4]);
					wb.setWxorder(values[5]);
					wb.setBzorder(values[6]);
					wb.setOpenid(values[7]);
					wb.setTradetype(values[8]);
					wb.setTradestatus(values[9]);
					wb.setBank(values[10]);
					wb.setCurrency(values[11]);
					wb.setTotalmoney(values[12]);
					wb.setRedpacketmoney(values[13]);
					wb.setWxrefund(values[14]);
					wb.setBzrefund(values[15]);
					wb.setRefundmoney(values[16]);
					wb.setRedpacketrefund(values[17]);
					wb.setRefundtype(values[18]);
					wb.setRefundstatus(values[19]);
					wb.setProductname(values[20]);
					wb.setBzdatapacket(values[21]);
					wb.setFee(values[22]);
					wb.setRate(values[23]);
					list.add(wb);
				}
				
			}
			if(list!=null && list.size()>0){
				for(WeixinBill wb : list){
					wxBillMapper.insert(wb);
				}
			}
		}
		
		long ms = System.currentTimeMillis();
		logger.info("下载对账单定时任务：(" + counter++ + ")" + new Date(ms));						
	}
}
